package com.baozun.houji.goods.adapter

import android.view.ViewTreeObserver
import androidx.databinding.DataBindingUtil
import com.baozun.houji.goods.R
import com.baozun.houji.goods.databinding.ItemHomeTopCategoryBinding
import com.baozun.houji.goods.model.bean.HomeGoldenBean
import com.blankj.utilcode.util.ThreadUtils
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

/**
 * @Title：三级分类适配器
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/10
 * @Version：1.0.0
 */
class HomeTopCategoryViewPagerAdapter(private var onItemChildClickListener: (model: HomeGoldenBean) -> Unit) :
    BaseBannerAdapter<MutableList<HomeGoldenBean>>() {

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.item_home_top_category
    }

    override fun bindData(
        holder: BaseViewHolder<MutableList<HomeGoldenBean>>?,
        data: MutableList<HomeGoldenBean>?,
        position: Int,
        pageSize: Int
    ) {
        var dataBinding: ItemHomeTopCategoryBinding? = holder?.let {
            DataBindingUtil.bind(it.itemView)
        }
        dataBinding?.rvCategory?.adapter = HomeTopCategoryChildAdapter(data, onItemChildClickListener)
        dataBinding?.rvCategory?.viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                ThreadUtils.runOnUiThreadDelayed({
                    dataBinding?.rvCategory?.adapter?.notifyDataSetChanged()
                }, 1000)
                dataBinding?.rvCategory?.viewTreeObserver?.removeOnGlobalLayoutListener(this)
            }
        })
    }

}